package com.qihunet.springbootjwt.utils;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author gary
 */
@Component
public final class RedisUtils {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 判断key是否存在
     * @param key 键值
     * @return 结果
     */
    public Boolean exists(String key) {
        return stringRedisTemplate.hasKey(key);
    }

    /**
     * 添加键值信息
     * @param key 键
     * @param value 值
     */
    public void set(String key, String value){
        stringRedisTemplate.opsForSet().add(key,value);
    }

    /**
     * 添加键值信息
     * @param key 键
     * @param value 值
     * @param interimExpiration 过期时间
     */
    public void set(String key, String value, Long interimExpiration) {
        set(key,value);
        stringRedisTemplate.expire(key,interimExpiration, TimeUnit.MINUTES);
    }
}
